package model.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import controller.util.ControllerHelper;

import model.pojo.*;
import model.util.*;



public class KhuyenMaiDAO {
	
	public static ArrayList<KhuyenMai> LayDSKhuyenMai()
	{
		ArrayList<KhuyenMai> _arr= new ArrayList<KhuyenMai>();
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            ResultSet result = helper.executeProcedure("{call sp_selectAllKhuyenMai()}", para);
            while(result.next())
            {
                KhuyenMai km = new KhuyenMai();
                km.set_maKhuyenMai(result.getInt("MaKhuyenMai"));
                km.setThongTinKM(result.getString("ThongTinKM"));
                _arr.add(km);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		
		return _arr;
	}
	
	//Thêm Khuyến Mãi
	public static boolean ThemKhuyenMai(KhuyenMai km)
	{
		boolean kq = false;
		DataHelper helper = new DataHelper();
		
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
	        para.add(km.getThongTinKM());
            String sp = ControllerHelper.getStoreProcedureString("ThemKhuyenMai");
            int kqIns = helper.executeInsProcedure(sp, para);
            if(kqIns>0)
            {
            	kq=true;
            }            
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return kq;
	}
	//kiem tra khuyen mai
	public static ArrayList<KhuyenMai> KiemTraKhuyenMai(String ten)
	{
		
		ArrayList<KhuyenMai> _arr= new ArrayList<KhuyenMai>();
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(ten);
            String sp = ControllerHelper.getStoreProcedureString("KiemTraKhuyenMai");
            ResultSet result = helper.executeProcedure(sp, para);
            while(result.next())
            {
                KhuyenMai km = new KhuyenMai();
                km.set_maKhuyenMai(result.getInt("MaKhuyenMai"));
                km.setThongTinKM(result.getString("ThongTinKM"));
                _arr.add(km);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		
		return _arr;
	}
	//xoa khuyen mai
	public static boolean XoaKhuyenMai(int ma)
	{
		boolean kq= false;
		DataHelper helper = new DataHelper();
        try
        {
            
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();	
            para.add(ma);
            String stored = ControllerHelper.getStoreProcedureString("XoaKM");
            //ResultSet result = helper.executeProcedure(stored, para);
            int kqDel = helper.executeInsProcedure(stored, para);
            if(kqDel !=-1)
            {	                            
                kq=true;
            }           
            //helper.close();
        }
        catch(Exception ex)
        {
        	//kq=false;
           //System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return kq;
	}
}
